/*
 *  @ProductName@
 *  Copyright (C) @CopyrightYear@
 *    National Institute of Advanced Industrial Science and Technology (AIST)
 *    Registration Number: @AISTRegistrationNumber@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.vuln.core.scap.nvd;

import jp.go.aist.six.util.search.Binding;
import jp.go.aist.six.util.search.Limit;
import jp.go.aist.six.util.search.Order;
import jp.go.aist.six.vuln.model.scap.vulnerability.Vulnerability;
import jp.go.aist.six.vuln.service.VulnerabilityService;
import jp.go.aist.six.vuln.service.VulnerabilityServiceException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Collection;
import java.util.List;



/**
 * @author  Akihito Nakamura, AIST
 * @version $Id: NvdService.java 200 2010-06-01 06:18:47Z nakamura5akihito $
 */
public class NvdService
implements VulnerabilityService
{

//    /**
//     * The sole instance.
//     */
//    private static NvdService  _instance = new NvdService();


    /**
     * The Spring application context specification.
     */
    private static final String _SPRING_APP_CONTEXT_
        = "/six-vuln_spring-app-context.xml";


    /**
     * The Spring application context.
     */
    private ApplicationContext  _springContext;



    /**
     * The data store sole instance.
     */
    private NvdStore  _store;



    /**
     * The XML processor sole instance.
     */
    private NvdXml  _xml;




    /**
     * Constructor.
     */
    public NvdService()
    {
    }



    /**
     */
    private ApplicationContext _getContext()
    throws Exception
    {
        if (_springContext == null) {
            _springContext = new ClassPathXmlApplicationContext( _SPRING_APP_CONTEXT_ );
            //throws BeansException
        }

        return _springContext;
    }



    /**
     */
    public NvdStore getStore()
    throws Exception
    {
        if (_store == null) {
            _store = (NvdStore)_getContext().getBean( "nvdStore" );
        }

        return _store;
    }



    /**
     */
    public NvdXml getXml()
    throws Exception
    {
        if (_xml == null) {
            _xml = (NvdXml)_getContext().getBean( "nvdXml" );
        }

        return _xml;
    }



//    ////////////////////////////////////////////////////////////////
//    //  static utility methods
//    ////////////////////////////////////////////////////////////////
//
//    /**
//     */
//    public static NvdStore getStore()
//    throws Exception
//    {
//        return _instance._getStore();
//    }
//
//
//    /**
//     */
//    public static NvdXml getXml()
//    throws Exception
//    {
//        return _instance._getXml();
//    }



    //**************************************************************
    //  VulnerabilityService
    //**************************************************************

    public Vulnerability getVulnerability(
                    final String id
                    )
    throws VulnerabilityServiceException
    {
        Vulnerability  v = null;
        try {
            v = getStore().get( Vulnerability.class, id );
        } catch (Exception ex) {
            throw new VulnerabilityServiceException( ex );
        }

        return v;
    }



    public Collection<Vulnerability> findVulnerability(
                    final Binding filter,
                    final List<Order> ordering,
                    Limit limit
                    )
    throws VulnerabilityServiceException
    {
        Collection<Vulnerability>  v = null;
        try {
            v = getStore().find( Vulnerability.class, filter, ordering, limit );
        } catch (Exception ex) {
            throw new VulnerabilityServiceException( ex );
        }

        return v;
    }



    public int countAllVulnerability()
    throws VulnerabilityServiceException
    {
        int  count = 0;
        try {
            count = getStore().countAll( Vulnerability.class );
        }  catch (Exception ex) {
            throw new VulnerabilityServiceException( ex );
        }

        return count;
    }



    public int countVulnerability(
                    final Binding filter
                    )
    throws VulnerabilityServiceException
    {
        int  count = 0;
        try {
            if (filter == null) {
                count = getStore().countAll( Vulnerability.class );
            } else {
                count = getStore().count( Vulnerability.class, filter );
            }
        }  catch (Exception ex) {
            throw new VulnerabilityServiceException( ex );
        }

        return count;
    }



    public Collection<String> getAllVulnerabilityID()
    throws VulnerabilityServiceException
    {
        Collection<?>  v = null;
        try {
            v = getStore().findIdentity( Vulnerability.class, null, null, null );
        } catch (Exception ex) {
            throw new VulnerabilityServiceException( ex );
        }

        @SuppressWarnings( "unchecked" )
        Collection<String>  ids = (Collection<String>)v;
        return ids;
    }
//    {
//        SearchCriteria  criteria = new SearchCriteria();
//        criteria.addProjection( new PropertyProjection( "persistentID") );
//
//        Collection<?>  v = null;
//        try {
//            v = getStore().search( Vulnerability.class, criteria );
//        } catch (Exception ex) {
//            throw new VulnerabilityServiceException( ex );
//        }
//
//        @SuppressWarnings( "unchecked" )
//        Collection<String>  ids = (Collection<String>)v;
//        return ids;
//    }



    public Collection<String> findVulnerabilityID(
                    final Binding filter
                    )
    throws VulnerabilityServiceException
    {
        Collection<?>  v = null;
        try {
            v = getStore().findIdentity( Vulnerability.class, filter, null, null );
        } catch (Exception ex) {
            throw new VulnerabilityServiceException( ex );
        }

        @SuppressWarnings( "unchecked" )
        Collection<String>  ids = (Collection<String>)v;
        return ids;
    }
//    {
//        SearchCriteria  idCriteria = new SearchCriteria();
//        idCriteria.addProjection( new PropertyProjection( "persistentID") );
//        idCriteria.setBinding( filter );
//
//        Collection<?>  v = null;
//        try {
//            v = getStore().search( Vulnerability.class, idCriteria );
//        } catch (Exception ex) {
//            throw new VulnerabilityServiceException( ex );
//        }
//
//        @SuppressWarnings( "unchecked" )
//        Collection<String>  ids = (Collection<String>)v;
//        return ids;
//    }

}
// NvdSercice

/* vim:set tabstop=4:set expandtab:set shiftwidth=4: */

